//! @file   lc_scope_guard.h
//! @brief  
//! @author Sayan Chaliha
//! @date   October, 2011
//!
//! 
#ifndef _LC_SCOPE_GUARD_H
#define _LC_SCOPE_GUARD_H

#include "libcyan_internal.h"
#include "lc_types.h"
#include "lc_delegate.h"
#include "lc_object.h"

namespace cyan {
    class CYAN_API ScopeGuard : public Object {
    public:
        ScopeGuard(const Delegate<void ()>& function) : function_(function),
            isActive_(true) {
        }
        
        virtual ~ScopeGuard() {
            if (this->isActive_ && this->function_) {
                this->function_();
            }
        }
        
        void disable() {
            this->isActive_ = false;
        }
        
    private:
        ScopeGuard() {
        }
        
        ScopeGuard(const ScopeGuard&) {
        }
        
        void operator =(const ScopeGuard&) {
        }
        
        Delegate<void ()> function_;
        Bool isActive_;
    };
}

#endif /* _LC_SCOPE_GUARD_H */
